﻿using System;
using System.IO;
using HNAlbum.Class;
using HNAlbum.Helper;
using HNAlbum.Logic.Entity;
using HNAlbum.Logic.Manager;

namespace HNAlbum
{
    public partial class ManageAlbum : BasePage
    {
        #region Private helpers
        /// <summary>
        /// Check which mode to build the page on
        /// </summary>
        private void checkMode()
        {
            //Just hide all panels at start
            pnlManageOption.Visible = false;
            pnlCreateAlbum.Visible = false;
            pnlEditAlbum.Visible = false;

            //Option mode
            if (Request.QueryString["mode"] == null)
            {
                pnlManageOption.Visible = true;
            }
            else
            {
                string mode = Request.QueryString["mode"];
                //New mode
                if (mode.Equals("new"))
                    pnlCreateAlbum.Visible = true;
                //Edit mode
                else if (mode.Equals("edit"))
                    pnlEditAlbum.Visible = true;
            }
        }

        #endregion

        #region Page events
        protected void Page_Init(object sender, EventArgs e)
        {
            //Register JS
            RegisterJS("plupload.js", "Plupload");
            RegisterJS("plupload.silverlight.js", "Plupload");
            RegisterJS("jquery.ui.plupload.js", "Plupload");
            //RegisterJS("jquery.plupload.queue.js", "Plupload");

            RegisterJS("ManageAlbum.js");

            //Register CSS
            RegisterCSS("jquery.ui.plupload.css");
            //RegisterCSS("jquery.plupload.queue.css");
        }
        /// <summary>
        /// Page load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //First load
            if (!IsPostBack)
            {
                //Clean the upload folder
                AlbumManager albumMgr = new AlbumManager(false);
                albumMgr.CleanUploadFolder();
            }

            //Check mode
            checkMode();
        }
        /// <summary>
        /// Handle moving back to option mode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gotoManageOption(object sender, EventArgs e)
        {
            Response.Redirect(Constant.PageURL.ManageAlbum);
        }
        /// <summary>
        /// Create a new Album
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCreateNewAlbum_Click(object sender, EventArgs e)
        {
            //Create skeleton album
            HNAlbumData newAlbum = new HNAlbumData();
            newAlbum.Name = Server.HtmlEncode(txtCreateAlbumName.Text);
            newAlbum.Description = Server.HtmlEncode(txtDescription.Text);
            newAlbum.Date = DateTime.Now;
            newAlbum.CreatedBy = CustomMasterPage.SessionCollection.User.CurrentUser.Login;

            //Retrieve uploaded files list
            DirectoryInfo tmpDir = new DirectoryInfo(Path.Combine(AppPhysicalPath, EntityBase.ApplicationFoler.Upload));
            FileInfo[] uploadedFiles = tmpDir.GetFiles();

            try
            {
                //Retrieve album creation settings
                newAlbum.AlbumSettings.CreateDefaultCaption = chkDefaultCaption.Checked;

                //Create the new album
                AlbumManager albumMgr = new AlbumManager(true);
                albumMgr.CreateAlbum(newAlbum, uploadedFiles);
            }
            catch (Exception ex)
            {
                SetPageMessage(ex.Message, WebControl.PageMessage.MessageType.Error);
                return;
            }

            //Show successfull message
            SetPageMessage("The Album has been created successfully!", WebControl.PageMessage.MessageType.Succeed);
        }
        #endregion
    }
}